$(document).ready(function () {
	// --- 绑定状态编辑点击事件 ---
	$('#listTBody').on('click', '.state', function () {
		$('#stateWin').show();
		var dataState = $(this).attr('dataState').split('|');
		$('#stateWin [name=state]').val(dataState[0]);
		$('#stateWin [name=stateIn]').val(dataState[1]);
		$('#stateWin [name=id]').val($(this).siblings('.id').text());
		$('#stateWin .id').text($(this).siblings('.id').text());
	});
	// --- 窗口取消按钮 ---
	$('.window .cancel').on('click', function () {
		$(this).parents('.window').eq(0).hide();
	});
	// --- 修改订单 state ---
	$('#stateWin .ok').on('click', function () {
		var $mask = loading();
		var $win = $(this).parents('.window').eq(0);
		$.ajax({type:'post',url:SITE_PATH+'admin/control/orderState',data:$(this).parents('.content').eq(0).find('select,input').serialize(),success:function (j) {
			if(j.result > 0) {
				getOrders();
				$win.hide();
			} else
				alert(j.msg);
			$mask.remove();
		}});
	});
	getOrders();
});
function getOrders() {
	$.ajax({type:'post',url:SITE_PATH+'admin/control/orderPull',success:function(j) {
		var listHtml = '';
		for(var k in j.pieces) {
			var piece = j.pieces[k];
			listHtml += '<tr><td class="id">'+piece.id+'</td><td>'+piece.address_sender+'</td><td>'+piece.phone_sender+'</td><td>'+piece.name_sender+'</td><td>'+piece.address_recipient+'</td><td>'+piece.phone_recipient+'</td><td>'+piece.name_recipient+'</td><td>'+piece.time_add+'</td><td'+((j.users[piece.user_id].phone==piece.phone_sender)?'':' class="wrong"')+'>'+j.users[piece.user_id].phone+'</td><td>'+piece.price_goods+'<br>(需'+piece.price_pop+')</td><td>'+piece.price+'</td><td>'+piece.type_pay+'</td><td>'+piece.type_product+'</td><td>'+piece.state_pay+'</td><td class="state" dataState="'+piece.state+'|'+piece.state_in+'">'+piece.state_name+'<br>('+piece.state_in_name+')</td><td>操作</td></tr>';
		}
		$('#listTBody').html(listHtml);
	}});
}

